﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using BLL;
using System.IO;

namespace PMS.Web
{
    public partial class Evelve : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string role = PageComm.GetUser(this.Response).role;
                if (!PageComm.IsEdit(ActionMenu.Evelve, role))
                {
                    FileUploadDeviceList.Visible = false;
                    FileUploadPlaneDiagram.Visible = false;
                    FileUploadProgress.Visible = false;
                    FileUploadReceipt.Visible = false;
                    FileUploadReport.Visible = false;
                    FileUploadSystemDiagram.Visible = false;
                    btn_DeviceList.Visible = false;
                    btn_PlaneDiagram.Visible = false;
                    btn_Progress.Visible = false;
                    btn_Receipt.Visible = false;
                    btn_Report.Visible = false;
                    btn_SystemDiagram.Visible = false;
                    gv_PMS_ProjectEvolve.Columns[gv_PMS_ProjectEvolve.Columns.Count - 2].Visible = false;
                    gv_PMS_ProjectEvolve.Columns[gv_PMS_ProjectEvolve.Columns.Count - 1].Visible = false;
                }
                string projectID = Request["projectID"];
                if (!string.IsNullOrEmpty(projectID))
                {
                    hid_projectID.Value = projectID;
                    DataLoad();
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private void DataLoadAttachment()
        {
            string projectID = hid_projectID.Value;
            PMS_ProjectEvolveAttachmentBLL bll = new PMS_ProjectEvolveAttachmentBLL();
            List<PMS_ProjectEvolveAttachment> list = bll.GetList(projectID);
            PMS_ProjectEvolveAttachment model = list.FirstOrDefault(p => p.orders == 1);
            if (model != null)
            {
                btn_DeviceList.CommandArgument = model.id.ToString();
                if (!string.IsNullOrEmpty(model.attachment))
                    lbtn_DeviceList.HRef = model.attachment;
            }
            model = list.FirstOrDefault(p => p.orders == 2);
            if (model != null)
            {
                btn_PlaneDiagram.CommandArgument = model.id.ToString();
                if (!string.IsNullOrEmpty(model.attachment))
                    lbtn_PlaneDiagram.HRef = model.attachment;
            }
            model = list.FirstOrDefault(p => p.orders == 3);
            if (model != null)
            {
                btn_SystemDiagram.CommandArgument = model.id.ToString();
                if (!string.IsNullOrEmpty(model.attachment))
                    lbtn_SystemDiagram.HRef = model.attachment;
            }
            model = list.FirstOrDefault(p => p.orders == 4);
            if (model != null)
            {
                btn_Progress.CommandArgument = model.id.ToString();
                if (!string.IsNullOrEmpty(model.attachment))
                    lbtn_Progress.HRef = model.attachment;
            }
            model = list.FirstOrDefault(p => p.orders == 5);
            if (model != null)
            {
                btn_Receipt.CommandArgument = model.id.ToString();
                if (!string.IsNullOrEmpty(model.attachment))
                    lbtn_Receipt.HRef = model.attachment;
            }
            model = list.FirstOrDefault(p => p.orders == 6);
            if (model != null)
            {
                btn_Report.CommandArgument = model.id.ToString();
                if (!string.IsNullOrEmpty(model.attachment))
                    lbtn_Report.HRef = model.attachment;
            }
        }

        /// <summary>
        /// 列表绑定
        /// </summary>
        /// <param name="projectID"></param>
        private void DataLoad()
        {
            try
            {
                string projectID = hid_projectID.Value;
                DataLoadAttachment();
                PMS_ProjectBLL pbll = new PMS_ProjectBLL();
                PMS_Project project = pbll.GetModel(Convert.ToInt32(projectID));
                lab_ProjectID.Text = project.projectID;
                lab_JDEID.Text = project.JDEID;
                lab_Customer.Text = project.customer;
                lab_CustomerTel.Text = project.customerTel;
                lab_ProjectName.Text = project.projectName;
                lab_ProjectType.Text = project.projectType;
                lab_SystemPath.Text = project.systemPath;
                lab_City.Text = project.city;

                PMS_ProjectEvolveBLL bll = new PMS_ProjectEvolveBLL();
                List<PMS_ProjectEvolve> liste = bll.GetList(projectID);
                PMS_ProjectEvolve pe = liste.OrderByDescending(p => p.completeDate).FirstOrDefault();
                if (pe.startDate != null && pe.startDate != DateTime.MinValue)
                {
                    int n = pe.orders;
                    if (n < 8)
                    {
                        n++;
                        lab_State.Text = liste.FirstOrDefault(p => p.orders == n).status;
                    }
                    else
                    {
                        lab_State.Text = pe.status;
                    }
                }
                this.gv_PMS_ProjectEvolve.DataSource = liste;
                this.gv_PMS_ProjectEvolve.DataBind();
            }
            catch (Exception ex)
            {
            }
        }

        protected void gv_PMS_ProjectEvolve_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            GridViewRow row = e.Row;
            if (row.RowType == DataControlRowType.DataRow)
            {
                PMS_ProjectEvolve model = row.DataItem as PMS_ProjectEvolve;
                string startDate = "";
                string planEndDate = "";
                string completeDate = "";
                if (model.startDate != null && model.startDate != DateTime.MinValue)
                {
                    startDate = model.startDate.ToString("yyyy-MM-dd");
                }
                if (model.planEndDate != null && model.planEndDate != DateTime.MinValue)
                {
                    planEndDate = model.planEndDate.ToString("yyyy-MM-dd");
                }
                if (model.completeDate != null && model.completeDate != DateTime.MinValue)
                {
                    completeDate = model.completeDate.ToString("yyyy-MM-dd");
                }
                bool isEdit = (row.RowIndex == gv_PMS_ProjectEvolve.EditIndex);
                if (isEdit == false)
                {
                    row.Cells[2].Text = startDate;
                    row.Cells[3].Text = planEndDate;
                    row.Cells[4].Text = completeDate;
                }
                else
                {
                    ((TextBox)row.Cells[2].Controls[0]).Text = startDate;
                    ((TextBox)row.Cells[3].Controls[0]).Text = planEndDate;
                    ((TextBox)row.Cells[4].Controls[0]).Text = completeDate;
                }
            }
        }

        protected void gv_PMS_ProjectEvolve_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gv_PMS_ProjectEvolve.EditIndex = e.NewEditIndex;
            DataLoad();
        }

        protected void gv_PMS_ProjectEvolve_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gv_PMS_ProjectEvolve.EditIndex = -1;
            DataLoad();
        }

        protected void gv_PMS_ProjectEvolve_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            PMS_ProjectEvolve pe = new PMS_ProjectEvolve();
            pe.id = Convert.ToInt32(e.Keys["id"]);
            try
            {
                if (e.NewValues["startDate"] != null)
                {
                    pe.startDate = Convert.ToDateTime(e.NewValues["startDate"]);
                }
            }
            catch
            {
                this.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>alert('开工时间格式错误');</script>");
                return;
            }
            try
            {
                if (e.NewValues["planEndDate"] != null)
                {
                    pe.planEndDate = Convert.ToDateTime(e.NewValues["planEndDate"]);
                }
            }
            catch
            {
                this.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>alert('预计结束时间格式错误');</script>");
                return;
            }
            try
            {
                if (e.NewValues["completeDate"] != null)
                {
                    pe.completeDate = Convert.ToDateTime(e.NewValues["completeDate"]);
                }
            }
            catch
            {
                this.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>alert('实际完成时间格式错误');</script>");
                return;
            }
            if (e.NewValues["principal"] != null)
                pe.principal = e.NewValues["principal"].ToString();
            PMS_ProjectEvolveBLL bll = new PMS_ProjectEvolveBLL();
            if (!bll.Update(pe))
            {
                this.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>alert('更新失败，请稍后重试');</script>");
                return;
            }
            gv_PMS_ProjectEvolve.EditIndex = -1;
            DataLoad();

        }

        protected void btn_DeviceList_Click(object sender, EventArgs e)
        {
            PMS_ProjectEvolveAttachmentBLL bll = new PMS_ProjectEvolveAttachmentBLL();
            PMS_ProjectEvolveAttachment model = new PMS_ProjectEvolveAttachment();
            model.id = Convert.ToInt32(btn_DeviceList.CommandArgument);
            if (string.IsNullOrEmpty(FileUploadDeviceList.FileName))
            {
                ClientScript.RegisterStartupScript(GetType(), "msg", "<script>alert('请选择上传的文件');</script>");
                return;
            }
            model.attachment = lab_ProjectID.Text.Trim() + "/" + model.id + FileUploadDeviceList.FileName;

            if (bll.Update(model))
            {
                if (!string.IsNullOrEmpty(FileUploadDeviceList.FileName))
                {
                    string path = System.AppDomain.CurrentDomain.BaseDirectory + "Upload\\Evelve\\" + lab_ProjectID.Text.Trim() + "\\";
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    path = path + model.id + FileUploadDeviceList.FileName;

                    FileUploadDeviceList.SaveAs(path);
                }
                DataLoadAttachment();
            }
        }

        protected void btn_PlaneDiagram_Click(object sender, EventArgs e)
        {
            PMS_ProjectEvolveAttachmentBLL bll = new PMS_ProjectEvolveAttachmentBLL();
            PMS_ProjectEvolveAttachment model = new PMS_ProjectEvolveAttachment();
            model.id = Convert.ToInt32(btn_PlaneDiagram.CommandArgument);
            if (string.IsNullOrEmpty(FileUploadPlaneDiagram.FileName))
            {
                ClientScript.RegisterStartupScript(GetType(), "msg", "<script>alert('请选择上传的文件');</script>");
                return;
            }
            else
            {
                model.attachment = lab_ProjectID.Text.Trim() + "/" + model.id + FileUploadPlaneDiagram.FileName;
            }
            if (bll.Update(model))
            {
                if (!string.IsNullOrEmpty(model.attachment))
                {
                    string path = System.AppDomain.CurrentDomain.BaseDirectory + "Upload\\Evelve\\" + lab_ProjectID.Text.Trim() + "\\";
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    path = path + model.id + FileUploadDeviceList.FileName;

                    FileUploadPlaneDiagram.SaveAs(path);
                }
                DataLoadAttachment();
            }
        }

        protected void btn_SystemDiagram_Click(object sender, EventArgs e)
        {
            PMS_ProjectEvolveAttachmentBLL bll = new PMS_ProjectEvolveAttachmentBLL();
            PMS_ProjectEvolveAttachment model = new PMS_ProjectEvolveAttachment();
            model.id = Convert.ToInt32(btn_SystemDiagram.CommandArgument);
            if (string.IsNullOrEmpty(FileUploadSystemDiagram.FileName))
            {
                ClientScript.RegisterStartupScript(GetType(), "msg", "<script>alert('请选择上传的文件');</script>");
                return;
            }
            else
            {
                model.attachment = lab_ProjectID.Text.Trim() + "/" + model.id + FileUploadSystemDiagram.FileName;
            }
            if (bll.Update(model))
            {
                if (!string.IsNullOrEmpty(model.attachment))
                {
                    string path = System.AppDomain.CurrentDomain.BaseDirectory + "Upload\\Evelve\\" + lab_ProjectID.Text.Trim() + "\\";
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    path = path + model.id + FileUploadDeviceList.FileName;

                    FileUploadSystemDiagram.SaveAs(path);
                }
                DataLoadAttachment();
            }
        }

        protected void btn_Progress_Click(object sender, EventArgs e)
        {
            PMS_ProjectEvolveAttachmentBLL bll = new PMS_ProjectEvolveAttachmentBLL();
            PMS_ProjectEvolveAttachment model = new PMS_ProjectEvolveAttachment();
            model.id = Convert.ToInt32(btn_Progress.CommandArgument);
            if (string.IsNullOrEmpty(FileUploadProgress.FileName))
            {
                ClientScript.RegisterStartupScript(GetType(), "msg", "<script>alert('请选择上传的文件');</script>");
                return;
            }
            else
            {
                model.attachment = lab_ProjectID.Text.Trim() + "/" + model.id + FileUploadProgress.FileName;
            }
            if (bll.Update(model))
            {
                if (!string.IsNullOrEmpty(model.attachment))
                {
                    string path = System.AppDomain.CurrentDomain.BaseDirectory + "Upload\\Evelve\\" + lab_ProjectID.Text.Trim() + "\\";
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    path = path + model.id + FileUploadDeviceList.FileName;

                    FileUploadProgress.SaveAs(path);
                }
                DataLoadAttachment();
            }
        }

        protected void btn_Receipt_Click(object sender, EventArgs e)
        {
            PMS_ProjectEvolveAttachmentBLL bll = new PMS_ProjectEvolveAttachmentBLL();
            PMS_ProjectEvolveAttachment model = new PMS_ProjectEvolveAttachment();
            model.id = Convert.ToInt32(btn_Receipt.CommandArgument);
            if (string.IsNullOrEmpty(FileUploadReceipt.FileName))
            {
                ClientScript.RegisterStartupScript(GetType(), "msg", "<script>alert('请选择上传的文件');</script>");
                return;
            }
            else
            {
                model.attachment = lab_ProjectID.Text.Trim() + "/" + model.id + FileUploadReceipt.FileName;
            }
            if (bll.Update(model))
            {
                if (!string.IsNullOrEmpty(model.attachment))
                {
                    string path = System.AppDomain.CurrentDomain.BaseDirectory + "Upload\\Evelve\\" + lab_ProjectID.Text.Trim() + "\\";
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    path = path + model.id + FileUploadDeviceList.FileName;

                    FileUploadReceipt.SaveAs(path);
                }
                DataLoadAttachment();
            }
        }

        protected void btn_Report_Click(object sender, EventArgs e)
        {
            PMS_ProjectEvolveAttachmentBLL bll = new PMS_ProjectEvolveAttachmentBLL();
            PMS_ProjectEvolveAttachment model = new PMS_ProjectEvolveAttachment();
            model.id = Convert.ToInt32(btn_Report.CommandArgument);
            if (string.IsNullOrEmpty(FileUploadReport.FileName))
            {
                ClientScript.RegisterStartupScript(GetType(), "msg", "<script>alert('请选择上传的文件');</script>");
                return;
            }
            else
            {
                model.attachment = lab_ProjectID.Text.Trim() + "/" + model.id + FileUploadReport.FileName;
            }
            if (bll.Update(model))
            {
                if (!string.IsNullOrEmpty(model.attachment))
                {
                    string path = System.AppDomain.CurrentDomain.BaseDirectory + "Upload\\Evelve\\" + lab_ProjectID.Text.Trim() + "\\";
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    path = path + model.id + FileUploadDeviceList.FileName;

                    FileUploadReport.SaveAs(path);
                }
                DataLoadAttachment();
            }
        }
    }
}